package ip

import (
	"errors"
	"fmt"
)

const (
	ICMPProtocol byte = 1
)

const (
	IPVersion4 byte = 4

	ihl         = 0
	tos         = 1
	totalLength = 2
	id          = 4
	flags       = 6
	fragment    = 6
	ttl         = 8
	protocol    = 9
	hCheck      = 10
	srcAddr     = 12
	dstAddr     = 16

	fixedHL = 20 // 固定header的长度(不包括可选项)
)

var (
	ErrIPCheckSum = errors.New("ip checksum field error")

	ErrEchoCheckSum = errors.New("echo checksum field error")
)

type IPV4Field struct {
	Version  uint8
	IHL      uint8
	TOS      uint8
	TL       uint16
	ID       uint16
	Flags    uint8
	FragOff  uint16
	TTL      uint8
	Protocol uint8
	HCheck   uint16
	SrcAddr  Address
	DstAddr  Address
	Options  []byte
	Padding  []byte
}

type Address uint32

func (a Address) String() string {
	return fmt.Sprintf("%d.%d.%d.%d",
		byte(a>>24), byte(a>>16), byte(a>>8), byte(a))
}
